// pages/product/SpellGroupMore.js
var Zan = require('../../dist/index.js');
let network = null;
let utils = null;
let api = null;
let app = getApp()
var start = 0;
function countdown(that) {
  for (var i = 0; i < that.data.tuanzhang_list.length; i++) {
    var EndTime = that.data.tuanzhang_list[i].endDate * 1000 || [];
    var NowTime = new Date().getTime();
    var total_micro_second = EndTime - NowTime || [];
    // 渲染倒计时时钟
    that.data.tuanzhang_list[i].clock = dateformat(total_micro_second)
    // that.setData({
    //   clock: dateformat(total_micro_second)
    // });
    if (total_micro_second <= 0) {
      that.data.tuanzhang_list[i].clock = "已经截止"
      //return;
    }
  }
  that.setData({
    tuanzhang_list: that.data.tuanzhang_list
  })

  // console.log(that.data.tuanzhang_list);
  if (total_micro_second > 0) {
    setTimeout(function () {
      total_micro_second -= 1000;
      countdown(that);
    }, 1000)
  }

}

// 时间格式化输出，如11:03 25:19 每1s都会调用一次
function dateformat(micro_second) {
  // 总秒数
  var second = Math.floor(micro_second / 1000);
  // 天数
  var day = Math.floor(second / 3600 / 24) < 10 ? '0' + Math.floor(second / 3600 / 24) : Math.floor(second / 3600 / 24);
  // 小时
  var hr = Math.floor(second / 3600 % 24) < 10 ? '0' + Math.floor(second / 3600 % 24) : Math.floor(second / 3600 % 24);
  // 分钟
  var min = Math.floor(second / 60 % 60) < 10 ? '0' + Math.floor(second / 60 % 60) : Math.floor(second / 60 % 60);
  // 秒
  var sec = Math.floor(second % 60) < 10 ? '0' + Math.floor(second % 60) : Math.floor(second % 60);
  let date = '';
  if (day == 0) {
    date = hr + ":" + min + ":" + sec;
  } else {
    date = day + ":" + hr + ":" + min + ":" + sec;
  }
  return date;
}

Page(Object.assign({}, Zan.Tab, Zan.TopTips,{

  /**
   * 页面的初始数据
   */
  data: {
    tuanzhang_list:[],
    spellGroupId:'',
    recordsTotal:0,
    products:{},
    iscantuan:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    let products = wx.getStorageSync('GroupDetailsData');

    this.setData({
      products: products,
      spellGroupId: options.spellGroupsId
    })
    console.log("============getStorageSync===============");
    console.log(this.data.products);
    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    start = 0;
    this.setData({
      tuanzhang_list: []
    })
    network._post_page_json(this, api.SpellGroupDetail, start, { id: this.data.spellGroupId }, success);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.tuanzhang_list.length < this.data.recordsTotal) {
      start = start + 20;
      network._post_page_json(this, api.SpellGroupDetail, start, { id: this.data.spellGroupId }, success);
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // },
  // gotoCantuan:function(e){
  //   var tuanitem = e.currentTarget.dataset.tuanitem;
  //   this.setData({
  //     spellGroupsId: tuanitem.spellGroupsId,
  //     sgCombId: tuanitem.sgCombId,
  //     nowclock: tuanitem.clock,
  //     userSpellGroupId: tuanitem.id,
  //     originator: tuanitem.originator
  //   })
  //   network._post_json(this, api.spellGroupsActiveDetail, { sgCombId: this.data.sgCombId, spellGroupsId: this.data.spellGroupId, status: 0, originator: this.data.originator }, FSuccess);
  // },


  chooseSpan: function (e) {
    var event = e.currentTarget.dataset.event;
    if (this.data.count <= 0) {
      this.showZanTopTips(this.prompt, '请先选择数量！');
      return false;
    }
    //跳到支付页
    this.setData({
      showDialog: false,
    });
    // 先判断此用户是否开过或参加过此团团
    // tuanzhang_list userid
    this.data.products.product.grouptype = 1
    if (this.data.products.product.ismygroup == 1) {
      this.setData({
        iscantuan: 1
      })
    }
    //是否为服务商品
    console.log(this.data.products.product.product.isService);
    if (this.data.products.product.product.isService == 1) {
      this.data.products.isService = "1";
    }
    //参团
    if (event == 2) {
      var tuanitem = e.currentTarget.dataset.tuanitem;
      this.data.products.product.unigroupId = tuanitem.unigroupId
      this.data.products.product.grouptype = 0
    }

    if (this.data.iscantuan == 1) {
      this.showZanTopTips(this.prompt, '你已经开或参过此团，请进入我的->我的拼团进行查看');
    } else {
      this.setData({
        showDialog: true,
      });
      try {
        wx.setStorageSync('GroupDetailsData', this.data.products);
      } catch (e) {
      }
      wx.navigateTo({
        url: '/pages/pay/SpellPay',
      })
    }
  }
}))

var success = function (that, res){
  if (res.data.data.groupdetail){
    that.setData({
      tuanzhang_list: res.data.data.groupdetail,
      recordsTotal: res.data.data.groupdetail.length
    })
    console.log(that.data.tuanzhang_list);
    countdown(that);
  }
  
}

var FSuccess = function (that, res) {
  if (res.data.data.specType1 && !res.data.data.specType2 && !res.data.data.specType2) {
    res.data.data.spellGroupsComb.specDetails = res.data.data.specType1 + ':' + res.data.data.spellGroupsComb.specDetails1
  }
  if (res.data.data.specType2) {
    res.data.data.spellGroupsComb.specDetails = res.data.data.specType1 + ':' + res.data.data.spellGroupsComb.specDetails1 + '   ' + res.data.data.specType2 + ':' + res.data.data.spellGroupsComb.specDetails2
  }
  if (res.data.data.specType3) {
    res.data.data.spellGroupsComb.specDetails = res.data.data.specType1 + ':' + res.data.data.spellGroupsComb.specDetails1 + '   ' + res.data.data.specType2 + ':' + res.data.data.spellGroupsComb.specDetails2 + '   ' + res.data.data.specType3 + ':' + res.data.data.spellGroupsComb.specDetails3
  }

  if (res.data.data.spellGroupsDetails && res.data.data.spellGroupsDetails.length > 0) {
    for (var i = 0; i < res.data.data.spellGroupsDetails.length; i++) {
      if (res.data.data.spellGroupsDetails[i].originator == res.data.data.spellGroupsDetails[i].userId) {
        //获取团长购买的数量
        res.data.data.count = res.data.data.spellGroupsDetails[i].orderProductDetail.count;
        res.data.data.pintuanprice = res.data.data.spellGroupsDetails[i].orderProductDetail.price2
        res.data.data.danmaiprice = res.data.data.spellGroupsDetails[i].orderProductDetail.price
        break;
      }
    }
  }
  if (res.data.data) {
    that.setData({
      groupsellDetail: res.data.data,
      specDetails: res.data.data.spellGroupsComb ? res.data.data.spellGroupsComb.specDetails : '',
      cantuanlist: res.data.data.spellGroupsDetails
    })

  }
  
  if (that.data.nowclock != "已经截止") {
    wx.navigateTo({
      url: '/pages/product/SpellGroupDetails?spellGroupId=' + that.data.spellGroupId + '&userSpellGroupId=' + that.data.userSpellGroupId + '&sgCombId=' + that.data.sgCombId + '&specName=' + that.data.specDetails + '&orderprice=' + that.data.groupsellDetail.pintuanprice + '&proprice=' + that.data.groupsellDetail.danmaiprice + '&count=' + that.data.groupsellDetail.count,
    })
  }


}